package com.ls.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClient {
    public static void main(String[] args) throws Exception {

        client();
    }


    public static void client() throws Exception{
        //得到一个通道
        SocketChannel socketChannel = SocketChannel.open();
        //设置非阻塞
        socketChannel.configureBlocking(false);
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);


        if (!socketChannel.connect(inetSocketAddress)){
            while (!socketChannel.finishConnect()){
                System.out.println("因为连接需要时间，客户端不会阻塞，可以做其他工作..");
            }
        }

        //连接成功
        String str = "hello 123";
        ByteBuffer byteBuffer = ByteBuffer.wrap(str.getBytes());

        //发送数据 将buffer数据写入 channel
        socketChannel.write(byteBuffer);
        System.in.read();
    }
}
